using UnityEngine;
using UnityEngine.UI;

public class DualJoystickTouchContoller : MonoBehaviour
{
	public Image leftJoystickBackgroundImage;

	public Image rightJoystickBackgroundImage;

	public bool leftJoystickAlwaysVisible;

	public bool rightJoystickAlwaysVisible;

	private Image leftJoystickHandleImage;

	private Image rightJoystickHandleImage;

	private LeftJoystick leftJoystick;

	private RightJoystick rightJoystick;

	private int leftSideFingerID;

	private int rightSideFingerID;

	private void Start()
	{
		if (leftJoystickBackgroundImage.GetComponent<LeftJoystick>() == null)
		{
			Debug.LogError("There is no LeftJoystick script attached to the Left Joystick game object.");
		}
		else
		{
			leftJoystick = leftJoystickBackgroundImage.GetComponent<LeftJoystick>();
			leftJoystickBackgroundImage.enabled = leftJoystickAlwaysVisible;
		}
		if (leftJoystick.transform.GetChild(0).GetComponent<Image>() == null)
		{
			Debug.LogError("There is no left joystick handle image attached to this script.");
		}
		else
		{
			leftJoystickHandleImage = leftJoystick.transform.GetChild(0).GetComponent<Image>();
			leftJoystickHandleImage.enabled = leftJoystickAlwaysVisible;
		}
		if (rightJoystickBackgroundImage.GetComponent<RightJoystick>() == null)
		{
			Debug.LogError("There is no RightJoystick script attached to Right Joystick game object.");
		}
		else
		{
			rightJoystick = rightJoystickBackgroundImage.GetComponent<RightJoystick>();
			rightJoystickBackgroundImage.enabled = rightJoystickAlwaysVisible;
		}
		if (rightJoystick.transform.GetChild(0).GetComponent<Image>() == null)
		{
			Debug.LogError("There is no right joystick handle attached to this script.");
			return;
		}
		rightJoystickHandleImage = rightJoystick.transform.GetChild(0).GetComponent<Image>();
		rightJoystickHandleImage.enabled = rightJoystickAlwaysVisible;
	}

	private void Update()
	{
	}

	private void FixedUpdate()
	{
		if (Input.touchCount <= 0)
		{
			return;
		}
		Touch[] touches = Input.touches;
		for (int i = 0; i < Input.touchCount; i++)
		{
			if (touches[i].phase == TouchPhase.Began)
			{
				if (touches[i].position.x < (float)(Screen.width / 2))
				{
					leftSideFingerID = touches[i].fingerId;
					if (!leftJoystick.joystickStaysInFixedPosition)
					{
						Vector3 position = leftJoystickBackgroundImage.rectTransform.position;
						position.x = touches[i].position.x + leftJoystickBackgroundImage.rectTransform.sizeDelta.x / 2f;
						position.y = touches[i].position.y - leftJoystickBackgroundImage.rectTransform.sizeDelta.y / 2f;
						position.x = Mathf.Clamp(position.x, leftJoystickBackgroundImage.rectTransform.sizeDelta.x, Screen.width / 2);
						position.y = Mathf.Clamp(position.y, 0f, (float)Screen.height - leftJoystickBackgroundImage.rectTransform.sizeDelta.y);
						leftJoystickBackgroundImage.rectTransform.position = position;
						leftJoystickBackgroundImage.enabled = true;
						leftJoystickBackgroundImage.rectTransform.GetChild(0).GetComponent<Image>().enabled = true;
					}
					else if (touches[i].position.x <= leftJoystickBackgroundImage.rectTransform.position.x && touches[i].position.x >= leftJoystickBackgroundImage.rectTransform.position.x - leftJoystickBackgroundImage.rectTransform.sizeDelta.x && touches[i].position.y >= leftJoystickBackgroundImage.rectTransform.position.y && touches[i].position.y <= leftJoystickBackgroundImage.rectTransform.position.y + leftJoystickBackgroundImage.rectTransform.sizeDelta.y)
					{
						leftJoystickBackgroundImage.enabled = true;
						leftJoystickBackgroundImage.rectTransform.GetChild(0).GetComponent<Image>().enabled = true;
					}
				}
				if (touches[i].position.x > (float)(Screen.width / 2))
				{
					rightSideFingerID = touches[i].fingerId;
					if (!rightJoystick.joystickStaysInFixedPosition)
					{
						Vector3 position2 = rightJoystickBackgroundImage.rectTransform.position;
						position2.x = touches[i].position.x + rightJoystickBackgroundImage.rectTransform.sizeDelta.x / 2f;
						position2.y = touches[i].position.y - rightJoystickBackgroundImage.rectTransform.sizeDelta.y / 2f;
						position2.x = Mathf.Clamp(position2.x, (float)(Screen.width / 2) + rightJoystickBackgroundImage.rectTransform.sizeDelta.x, Screen.width);
						position2.y = Mathf.Clamp(position2.y, 0f, (float)Screen.height - rightJoystickBackgroundImage.rectTransform.sizeDelta.y);
						rightJoystickBackgroundImage.rectTransform.position = position2;
						rightJoystickBackgroundImage.enabled = true;
						rightJoystickBackgroundImage.rectTransform.GetChild(0).GetComponent<Image>().enabled = true;
					}
					else if (touches[i].position.x <= rightJoystickBackgroundImage.rectTransform.position.x && touches[i].position.x >= rightJoystickBackgroundImage.rectTransform.position.x - rightJoystickBackgroundImage.rectTransform.sizeDelta.x && touches[i].position.y >= rightJoystickBackgroundImage.rectTransform.position.y && touches[i].position.y <= rightJoystickBackgroundImage.rectTransform.position.y + rightJoystickBackgroundImage.rectTransform.sizeDelta.y)
					{
						rightJoystickBackgroundImage.enabled = true;
						rightJoystickBackgroundImage.rectTransform.GetChild(0).GetComponent<Image>().enabled = true;
					}
				}
			}
			if (touches[i].phase == TouchPhase.Ended)
			{
				if (touches[i].fingerId == leftSideFingerID)
				{
					leftJoystickBackgroundImage.enabled = leftJoystickAlwaysVisible;
					leftJoystickHandleImage.enabled = leftJoystickAlwaysVisible;
				}
				if (touches[i].fingerId == rightSideFingerID)
				{
					rightJoystickBackgroundImage.enabled = rightJoystickAlwaysVisible;
					rightJoystickHandleImage.enabled = rightJoystickAlwaysVisible;
				}
			}
		}
	}
}
